import akshare as ak
import pandas as pd
import psycopg2
import time

# 数据库连接参数
## dbname: the database name
## database: the database name (only as keyword argument)
## user: user name used to authenticate
## password: password used to authenticate
## host: database host address (defaults to UNIX socket if not provided)
## port: connection port number (defaults to 5432 if not provided)
conn = psycopg2.connect(database="finance",
                        user="postgres", password="123456",
                        host="127.0.0.1", port="5432")
cur = conn.cursor()  # 创建指针对象

cur.execute("SELECT code, sname FROM stock_market.stock")
datas = cur.fetchall()
for item in datas:
    time.sleep(0.1)
    code = item[0]
    sname = item[1]
    print(item[0], item[1])
    stock_df = ak.stock_zh_a_hist(code, period="daily", adjust='qfq')
    stock_file_name = "{}_{}_{}.csv".format("qfq", code, sname)
    stock_df.to_csv(stock_file_name)